package com.example.map_navi_plugin;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;


import com.kayoxu.androidlib.dialog.timepicker.KayoTimePicker;

import java.io.File;


public class NaviUtils {


    public interface NaviResult {
        void onClick();
    }


    private static final String baiduMap = "com.baidu.BaiduMap";
    private static final String aMap = "com.autonavi.minimap";
    private static final String qqMap = "com.tencent.map";
    private static final String googleMap = "com.google.android.apps.maps";


    public static void showNavi(Activity activity, String data) {
        NaviDialog naviDialog = new NaviDialog(activity);

        if (isAppInstalled(activity, baiduMap)) {
            naviDialog.addView("百度导航", () -> {
                gotoBaiduMap(activity, GsonUtils.gsonToBean(data, MapData.LocationData.class));
            });
        }
        if (isAppInstalled(activity, googleMap)) {
            naviDialog.addView("谷歌导航", () -> {
                gotoGoogleMap(activity, GsonUtils.gsonToBean(data, MapData.LocationData.class));
            });
        }
        if (isAppInstalled(activity, aMap)) {
            naviDialog.addView("高德导航", () -> {

            });
        }
        if (isAppInstalled(activity, qqMap)) {
            naviDialog.addView("腾讯导航", () -> {

            });
        }

        naviDialog.show();


//        KayoTimePicker timePicker = new KayoTimePicker(activity);

    }

    private static void gotoGoogleMap(Activity activity, MapData.LocationData data) {


//        Uri gmmIntentUri = Uri.parse("google.navigation:q="
//                + (0 == data.getLng() && 0 == data.getLat() ? "" : data.getLat() + "," + data.getLng() + ", + ")
//                + data.getAddress());

        Uri gmmIntentUri = Uri.parse("google.navigation:q="
                + (0 == data.getLng() && 0 == data.getLat() ? data.getAddress() : data.getLat() + "," + data.getLng()));

        Intent mapIntent = new Intent(Intent.ACTION_VIEW,
                gmmIntentUri);
        mapIntent.setPackage(googleMap);
        activity.startActivity(mapIntent);
    }

    private static void gotoBaiduMap(Activity activity, MapData.LocationData data) {
        try {
            Uri uri = Uri.parse("baidumap://map/direction?mode=driving&" +
                    "destination=latlng:" + data.getLat() + "," + data.getLng() +
                    "|name:" + data.getAddress() + //终点的显示名称
                    "&coord_type=gcj02");//坐标 （百度同样支持他自己的db0911的坐标，但是高德和腾讯不支持）
            Intent intent = new Intent();
            intent.setPackage(baiduMap);
            intent.setData(uri);

            activity.startActivity(intent);
        } catch (Exception e) {
//            Logger.e(TAG, e.getMessage());
        }
    }


    private static boolean isInstallPackage(String packageName) {
        return new File("/data/data/" + packageName).exists();
    }

    private static boolean isAppInstalled(Activity activity, String packageName) {
        PackageInfo packageInfo;
        try {
            packageInfo = activity.getPackageManager().getPackageInfo(packageName, 0);
        } catch (PackageManager.NameNotFoundException e) {
            packageInfo = null;
            e.printStackTrace();
        }
        if (packageInfo == null) {
            return isInstallPackage(packageName);
        } else {
            return true;
        }
    }
}
